package com.gljx.web.controller;

import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.User;
import com.gljx.web.Entity.WithholdItem;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.WithholdItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class WithholdItemController {
	
	@Autowired
	private WithholdItemService withholdItemService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private CustomLogService customLogService;
	
	
	/**
	 * 查询扣款项目
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selectWithholdItem.html/look.action")
	public String selWithholItem(HttpServletRequest request,@RequestParam(value = "type",required = false,defaultValue = "") String type) throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		//查询扣款的项目
        List<WithholdItem> withholdItemList=null;
        withholdItemList=withholdItemService.selectWithholdItem(user.getFcompanyid());
        request.setAttribute("withholdItemList", withholdItemList);
        request.setAttribute("type",type);
		return "backHome/basicData/financeData/withholdItem/withholdItemList";
	}
	/**
	 * 根据名称查询扣款项目信息
	 * @param request
	 * @param fnameL2
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selectWithholdItemByFname.html")
	public String selectWithholdItemByFname(HttpServletRequest request,String fnameL2)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		//查询扣款的项目
        List<WithholdItem> withholdItemList=null;
        withholdItemList=withholdItemService.selectWithholdItemByFname(fnameL2, user.getFcompanyid());
        request.setAttribute("withholdItemList", withholdItemList);
        request.setAttribute("fnameL2", fnameL2);
		return "backHome/basicData/financeData/withholdItem/withholdItemList";
	}
	/**
	 * 跳转到增加扣款项目页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoAddWithholdItem.html")
	public String intoAddWithholdItem(HttpServletRequest request,String type)throws Exception{
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取部门
	    List<Department>departments = departmentService.queryAdminByController(user.getFcompanyid());
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  //获取上一个编码
        /*String number = withholdItemService.selectFnumber(user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("SFWH");
	    request.setAttribute("department_withholdItem", department);
	    request.setAttribute("departments_withholdItem", departments);
	    request.setAttribute("fnumber", fnumber);
	    request.setAttribute("type",type);
		return "backHome/basicData/financeData/withholdItem/addWithholdItem";
	}
	
	/**
	 * 添加扣款项目
	 * @param request
	 * @param withholdItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addWithholdItem.html")
	public String addWithholdItem(HttpServletRequest request,WithholdItem withholdItem,
						HttpServletResponse response)throws Exception{
		//获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        /*获取uuid*/
        withholdItem.setFid(GetFid.UUIDIntoFid());
        withholdItem.setCfficompanyid(user.getFcompanyid());
        withholdItem.setFcreatorid(user.getFid());
        withholdItem.setFcreatetime(createTime);
        withholdItem.setFcontrolunitid(user.getFcompanyid());
        withholdItem.setCreateName(user.getName());
        int row = withholdItemService.addWithholdItem(withholdItem);
        String result="";
        if(row > 0){
        	result ="1";
		}
        customLogService.addLog("财务基础资料", createTime + "对扣款项目进行操作", null, user.getFid());
        return "redirect:/DrivingSys/intoAddWithholdItem.html?type="+result;
	}
	
	
	/**
	 * 批量查询扣款项目
	 * @createTime 2017-11-22 3:06 pm
	 * @author zyq.
	 * @param fid 选中的ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/selectWithHoldItemByIds")
	public List<WithholdItem> selectWithHoldItemByIds(String fid){
		List<WithholdItem> lItems = new ArrayList<>();
		String [] fids = fid.split(",");
		for(int i=0;i<fids.length;i++) {
			WithholdItem withholdItem = withholdItemService.queryWithholdItemByFid(fids[i]);
			lItems.add(withholdItem);
		}
		return lItems;
	}
	
	
	
	
	/**
	 * 跳转到修改扣款项目信息页面
	 * @param request
	 * @param fid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoUpdateWithholdItem.html")
	public String intoUpdateWithholdItem(HttpServletRequest request,
				@RequestParam(value="fid",required=false) String fid)throws Exception{		
		//获取当前登录人的信息
		User user = (User) request.getSession().getAttribute("user");
		//根据fid查询单条扣款项目信息带到修改页面中去
		WithholdItem withholdItem=null;
		if(fid!=null && !fid.equals("")){
			withholdItem=withholdItemService.queryWithholdItemByFid(fid);
		}
		//获取部门
	    List<Department>departments = departmentService.queryAdminByController(user.getFcompanyid());
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    request.setAttribute("department_withholdItem", department);
	    request.setAttribute("departments_withholdItem", departments);
	    request.setAttribute("update_withholdItem", withholdItem);
		return "backHome/basicData/financeData/withholdItem/updateWithholdItem";
	}
	/**
	 * 修改扣款项目信息
	 * @param request
	 * @param withholdItem
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateWithholdItem.html")
	public String updateWithholdItem(HttpServletRequest request,WithholdItem withholdItem) throws Exception{
		//获取当前登录人的信息
		User user = (User) request.getSession().getAttribute("user");
		//公司id
		withholdItem.setCfficompanyid(user.getFcompanyid());
        //控制单元id
		withholdItem.setFcontrolunitid(user.getFcompanyid());
		//获取当前修改时间
        String updateTime = TimeFormat.timeFormat(new Date());
        //设置最后修改时间
        withholdItem.setFlastupdatetime(updateTime);
        //设置修改人id
        withholdItem.setFlastupdateuserid(user.getFid());
        //修改
        int row = withholdItemService.updateWithholdItemByFid(withholdItem);
        String result="";
        if(row > 0){
        	result ="1";
		}
        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对扣款项目进行更新", null, user.getFid());
		return "redirect:/DrivingSys/selectWithholdItem.html/look.action?type="+result;
	}
	/**
	 * 根据删除fid删除扣款项目信息
	 * @param request
	 * @param deleteId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteWithholdItem.html")
	public String deleteWithholdItem(HttpServletRequest request, String[] deleteId,String fid)throws Exception{
		User user = (User) request.getSession().getAttribute("user");
        //删除记录
		if(fid==null || fid.equals("")){
			for (int i = 0; i < deleteId.length; i++) {
	            /*过滤为空的删除操作*/
	            if (!deleteId[i].equals("")&&deleteId[i]!=null) {
	            	withholdItemService.deleteByPrimaryKey(deleteId[i]);
	            }
	        }	
		}else{
        	withholdItemService.deleteByPrimaryKey(fid);
        }
        
        //添加日志
        customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date()) + "对扣款项目进行删除", null, user.getFid());
		return "redirect:/DrivingSys/selectWithholdItem.html/look.action";
	}

	/**
	 * 扣款项目 导出excel
	 * @param response
	 * @param request
	 * @param fnameL2
	 */
	@RequestMapping(value = "/importWitholdItem")
	public void importWitholdItem(HttpServletResponse response, HttpServletRequest request, String fnameL2){
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
		List<WithholdItem> withholdItems = withholdItemService.selectWithholdItemByFname(fnameL2,user.getFcompanyid());
		for (WithholdItem withholdItem : withholdItems){
			data.add(withholdItem.getFnumber());
			data.add(withholdItem.getFnameL2());
			data.add(withholdItem.getFsimplename());
			data.add(withholdItem.getFdescriptionL2());
			data.add(withholdItem.getFcreatetime());
			data.add(withholdItem.getFlastupdatetime());
			data.add(withholdItem.getCreateName());
			data.add(withholdItem.getUpdateName());
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间", "创建者", "修改人"};
		//长度
		Integer length[] = {9000, 5000, 5000, 5000, 5000, 5000, 3000, 3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "扣款项目" + TimeFormat.getNowTime2() + ".xls", "扣款项目", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
